package edu.guigu.appquickstart.recyclerview.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @author yuanc
 * @Description: (用一句话描述)
 * @date 2025/8/6 11:18
 */
public class PersonBean {
    private String author;
    private String desc;
    private String url;
    private String title;
    // 是否选中
    private boolean isChecked = false;
    // 是否显示(所有Person共享)
    public static boolean isShow = false;

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static ArrayList<PersonBean> loadData(Context context) {
        // 读取
        String json = load(context);
        // 解析
        Gson gson = new Gson();
        TypeToken<ArrayList<PersonBean>> typeToken = new TypeToken<>() {};
        ArrayList<PersonBean> personBeans = gson.fromJson(json, typeToken.getType());

        return personBeans;
    }

    private static String load(Context context){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // Assets资源文件读取
            InputStream inputStream = context.getAssets().open("persons.json");
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
            br.close();
            isr.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
